package org.jl.main.util;

import org.springframework.beans.BeanUtils;
import org.springframework.lang.NonNull;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class UtilForBean {

    public static <T> T beanCopy(@NonNull Object obj, Class<T> clazz) {
        try {
            T t = clazz.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(obj, t);
            return t;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                 NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> deepBeanCopy(@NonNull List<?> list, @NonNull Class<T> clazz) {
        return list.stream().map(obj -> beanCopy(obj, clazz)).toList();
    }

}
